<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 汪利东,2016-04-12 10:33
// +----------------------------------------------------------------------

namespace Admin\Controller;


class PayBuyController extends BaseController
{
    public function index()
    {
        $mapstr='a.type = 0 and a.state_order != 0';
        $pay_id = I('get.pay');

        if (!empty($pay_id)) {
            if($mapstr!=''){
                $mapstr.=' and ';
            }
            $mapstr.='(a.pay_id ='.$pay_id.')';
        }
        $keywords = I('get.keywords');
        if (!empty($keywords)) {
            if($mapstr!=''){
                $mapstr.=' and ';
            }
            $mapstr.='(a.pay_id like "%'.$keywords.'%" or b.card_id="'.$keywords.'")';
        }
        $interval_time = I('get.interval_time');
        if (!empty($interval_time)) {
            $time=explode('~',$interval_time);
            $time[0]=$time[0].' 00:00:00';
            $time[1]=$time[1].' 23:59:59';
            if($mapstr!=''){
                $mapstr.=' and ';
            }
            $mapstr.=' a.create_time >"'.$time[0].'" and a.create_time < "'.$time[1].'"';
        }

        $count = D('PayBook')->alias('a')->where($mapstr)->join('card b on a.user_id=b.user_id','left')->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count, C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $where['keywords'] = $keywords;
        foreach ($where as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出


        $data = D('PayBook')->alias('a')
            ->relation(true)
            ->where($mapstr)->join('card b on a.user_id=b.user_id','left')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('a.order_id desc')->field('a.*,b.card_id')
            ->select();
        if(I("get.execl")==1){
            $data = D('PayBook')->alias('a')
                ->relation(true)
                ->where($mapstr)->join('card b on a.user_id=b.user_id','left')
                ->order('a.order_id desc')->field('a.*,b.card_id')
                ->select();
            $data = $this->format($data);
            $file = excel_export($data);
            ouput_Browser($file);
        }else{
            $this->data = $data;
            $this->display();
        }
    }

    private function format($data)
    {
        return array_map(function($item){
            $data['订单ID'] = $item['order_id'];
            $data['用户ID'] = $item['user_id'];
            $data['订单总金额'] = $item['money'];
            $data['运费'] = $item['price_freight'];
            $data['类型'] = $item['type'] ? '借书' : '买书';
            $data['收货信息'] = $item['address'];
            $stateList = ['未支付','已支付未发货','配送中','已收货'];
            $data['状态'] = $stateList[$item['state_order']];
            $data['打印'] = $item['is_print'] == 2 ? '已打印' : '未打印';
            $data['支付号'] = $item['pay_id'];
            $data['是否免运费'] = $item['is_freightfree'] ? '是' : '否';
            $data['折扣'] = $item['discount_buy'];
            $data['退款状态'] = $item['is_refund'] ? '已退款' : '未退款';
            $data['销售渠道'] = $item['is_online'] ? '线下' : '微信';
            $data['创建时间'] = $item['create_time'];
            $data['备注'] = $item['order_remark'];
            return $data;
        },$data);
    }
}